﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;

namespace media
{
    /// <summary>
    /// UserControlMediaElement.xaml 的交互逻辑
    /// </summary>
    public partial class UserControlMediaElement : UserControl
    {
        public UserControlMediaElement()
        {
            InitializeComponent();
        }

        private void SetDisplayMessage(object sender, EventArgs e)
        {
            if (media1.NaturalDuration.HasTimeSpan)
            {
                TimeSpan currentPositionTimeSpan = media1.Position;

                string currentPosition = string.Format("{0:00}:{1:00}:{2:00}", (int)currentPositionTimeSpan.TotalHours, currentPositionTimeSpan.Minutes, currentPositionTimeSpan.Seconds);

                //TimeSpan totaotp = media1.NaturalDuration.TimeSpan;
                //string totalPostion = string.Format("{0:00}:{1:00}:{2:00}", (int)totaotp.TotalHours, totaotp.Minutes, totaotp.Seconds);

                currentPositionTime.Text = currentPosition;
                sliderPosition.Value = media1.Position.TotalSeconds;
            }
        }

        public System.Uri mysource;

        private bool ispause = true;
        private int angle = 0;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (ispause)
            {
                this.media1.Pause();
                ispause = !ispause;
                this.pause.Content = "play";
            }
            else
            {
                this.media1.Play();
                ispause = !ispause;
                this.pause.Content = "pause";
            }
        }

        private void media1_MediaEnded(object sender, RoutedEventArgs e)
        {
            media1.Position = new TimeSpan(0);
            media1.Play();
        }

        private void Button_Click_rotate(object sender, RoutedEventArgs e)
        {
            //media1.RenderTransform.d
            angle = angle + 90;
            media1.RenderTransform = new RotateTransform(angle);
        }

        private DispatcherTimer timer;

        public void MediaOpened()
        {
            if (!tmrProgress.IsEnabled)
            {
                tmrProgress.Start();
            }

            this.media1.Source = mysource;
            this.media1.Play();

            //sliderPosition.Maximum = media1.NaturalDuration.TimeSpan.TotalSeconds;
            //timer = new DispatcherTimer();
            //timer.Interval = TimeSpan.FromSeconds(1);
            //timer.Tick += new EventHandler(timer_tick);
            //timer.Start();
        }

        private void media1_MediaOpened(object sender, RoutedEventArgs e)
        {
            sliderPosition.Minimum = 0;
            sliderPosition.Maximum = media1.NaturalDuration.TimeSpan.TotalSeconds;
            TimeSpan totaotp = media1.NaturalDuration.TimeSpan;
            videoAllTime.Text = "/" + string.Format("{0:00}:{1:00}:{2:00}", (int)totaotp.TotalHours, totaotp.Minutes, totaotp.Seconds);
            currentPositionTime.Text = "00:00:00";

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timer_tick);
            timer.Start();
        }

        private DispatcherTimer tmrProgress = new DispatcherTimer();

        private void timer_tick(object sender, EventArgs e)
        {
            sliderPosition.Value = media1.Position.TotalSeconds;
        }

        private TimeSpan ts = new TimeSpan();

        //public ProgressConverter PrgConverter = new ProgressConverter();
        private void sliderPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            //mediaElement.Stop();
            //media1.Position = TimeSpan.FromSeconds(sliderPosition.Value);
            //timer1.Enabled = false;
            //mediaElement.Play();

            //timer1.Enabled = false;
            ts = TimeSpan.FromSeconds(e.NewValue);
            string currentPosition = string.Format("{0:00}:{1:00}:{2:00}", (int)ts.TotalHours, ts.Minutes, ts.Seconds);

            currentPositionTime.Text = currentPosition;
        }

        private void playProgressSlider_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            media1.Position = ts;
            //timer1.Enabled = false;
            timer1.Stop();
        }

        private void Button_Click_test(object sender, RoutedEventArgs e)
        {
            //timer1.Enabled = false;
            timer1.Stop();
            int i = 10;
            if (this.t1.Text != "")
            {
                i = Convert.ToInt32(this.t1.Text);
            }
            media1.Position = media1.Position + TimeSpan.FromSeconds(i);
        }

        private void grid1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            //if(e.Key){}
        }

        private void Button_Click_back(object sender, RoutedEventArgs e)
        {
            //timer1.Enabled = false;
            timer1.Stop();
            int i = 10;
            if (this.t1.Text != "")
            {
                i = Convert.ToInt32(this.t1.Text);
            }
            media1.Position = media1.Position - TimeSpan.FromSeconds(i);
        }

        private void media1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            //if()
        }

        private void sliderPosition_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Right)
            {
                int i = 10;
                if (this.t1.Text != "")
                {
                    i = Convert.ToInt32(this.t1.Text);
                }
                media1.Position = media1.Position + TimeSpan.FromSeconds(i);
            }
            else if (e.Key == System.Windows.Input.Key.Left)
            {
                int i = 10;
                if (this.t1.Text != "")
                {
                    i = Convert.ToInt32(this.t1.Text);
                }
                media1.Position = media1.Position - TimeSpan.FromSeconds(i);
            }
        }

        private TimeSpan tt;
        private System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();

        private void Button_Click_review(object sender, RoutedEventArgs e)
        {
            int i = 10;
            if (this.t1.Text != "")
            {
                i = Convert.ToInt32(this.t1.Text);
            }
            //media1.Position = media1.Position + TimeSpan.FromSeconds(i);
            tt = media1.Position;

            timer1.Interval = i * 1000; // 设置timer时间，单位是毫秒
            //timer1.Enabled = true; // 启动 timer
            timer1.Start();
            timer1.Tick += timer1_Tick;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //i++;
            //this.label1.Text = (i).ToString();
            media1.Position = tt - TimeSpan.FromSeconds(3);
        }
    }
}